package com.mynotes.notes;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.mynotes.notes.R;
import com.mynotes.notes.Regist.MyAsyncTask;
import com.mynotes.notes.utils.Init;
import com.mynotes.notes.utils.UploadMsgUtil;
import com.zxing.activity.CaptureActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity implements OnClickListener {
	TranslateAnimation left, right;
	Animation up, down;
	ImageView imagevu;

	private EditText name, pwd;
	private TextView regist, lost_pwd;
	private Button login;
	private ImageView scanCode;
	private String isExit;

	private MyAsyncTask mat1;
	private List<NameValuePair> params;
	private String result;
	private static String postMsgUrl = "http://hua.unohacha.com/contro.php?action=ck_login";
	private static final int scanCodeLogin = 1;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);

		isExit = getIntent().getStringExtra("exit");

		imagevu = (ImageView) findViewById(R.id.run_image);
		runAnimation();

		scanCode = (ImageView) findViewById(R.id.login_scan);
		name = (EditText) findViewById(R.id.login_name);
		pwd = (EditText) findViewById(R.id.login_pwd);
		regist = (TextView) findViewById(R.id.regist);
		lost_pwd = (TextView) findViewById(R.id.lost_pwd);
		login = (Button) findViewById(R.id.login_button);

		name.setText("13023687239");
		pwd.setText("123456");

		scanCode.setOnClickListener(this);
		regist.setOnClickListener(this);
		lost_pwd.setOnClickListener(this);
		login.setOnClickListener(this);
	}

	private void clearData() {
		Init.user_id = null;
		Init.getNotes.clear();
		Init.contactFriends.clear();
		Init.newFriends.clear();
		Init.oldFriends.clear();
		Init.groups.clear();
	}

	@Override
	public void onClick(View arg0) {
		String username;
		String password;
		Intent intent;
		switch (arg0.getId()) {
		case R.id.login_scan:
			Toast.makeText(this, "扫码登录", 1500).show();
			intent = new Intent(this, CaptureActivity.class);
			startActivityForResult(intent, scanCodeLogin);
			break;
		case R.id.regist:
			intent = new Intent(this, Regist.class);
			startActivity(intent);
			// finish();
			break;
		case R.id.lost_pwd:
			intent = new Intent(this, Setting_code.class);
			startActivity(intent);
			break;
		case R.id.login_button:
			username = name.getText().toString();
			password = pwd.getText().toString();

			// 构造POST方法的{name:value} 参数对
			params = new ArrayList<NameValuePair>();
			// 将参数传入post方法中
			params.add(new BasicNameValuePair("username", username));// 用户ID-15257183714
			params.add(new BasicNameValuePair("password", password));// 账户密码-123456
			mat1 = new MyAsyncTask(params, postMsgUrl);
			mat1.execute(postMsgUrl);
			break;

		default:
			break;
		}

	}

	// 异步加载
	class MyAsyncTask extends AsyncTask<String, Integer, String> {
		private List<NameValuePair> params;
		private String url;

		private MyAsyncTask(List<NameValuePair> params, String url) {
			this.params = params;
			this.url = url;
		}

		protected String doInBackground(String... arg0) {
			result = UploadMsgUtil.uploadMsg(params, url);
			return result;
		}

		protected void onPostExecute(String result) {
			Log.v("result", "登录返回结果——" + result);
			parsonJson(result);
			super.onPostExecute(result);
		}

	}

	private void parsonJson(String result) {
		try {
			JSONObject obj = new JSONObject(result);
			int getResult = obj.getInt("result");
			int getLimit = obj.getInt("limit");
			String user_id = obj.getString("user_id");
			if (0 == getResult) {//TODO账号登录返回id=null
				Toast.makeText(Login.this, "登录成功！", 1500).show();
				clearData();
				Intent intent = new Intent(Login.this, Main.class);
				
				Init.user_id = user_id;
				Init.user_limit = getLimit;
				Log.v("result", "登录成功！+inti_user_id" +Init.user_id+"---Init.user_limit "+Init.user_limit );
				startActivity(intent);
				finish();
			} else {
				Toast.makeText(Login.this, "登录失败！", 1500).show();
			}
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == scanCodeLogin && resultCode == RESULT_OK) {

			if (null != data) {
				Bundle bundle = data.getExtras();
				String scanResult = bundle.getString("result");

				// 构造POST方法的{name:value} 参数对
				params = new ArrayList<NameValuePair>();
				// 将参数传入post方法中
				params.add(new BasicNameValuePair("user_id", scanResult));// 用户ID-15257183714
				mat1 = new MyAsyncTask(params, postMsgUrl);
				mat1.execute(postMsgUrl);
				// if (scanResult.equals("123456")) {
				// clearData();
				// Intent intent2 = new Intent(Login.this, Main.class);
				// intent2.putExtra("result", scanResult);
				// startActivity(intent2);
				// finish();
				// }
				Toast.makeText(Login.this, "扫码结果：" + scanResult, 1000).show();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		if (null != isExit && "exit".equals(isExit)) {
			Log.v("result", "isExit:" + isExit);
			Init.app.onTerminate();
		} else {
			finish();
		}
		super.onBackPressed();
	}

	public void runAnimation() {
		down = AnimationUtils.loadAnimation(Login.this, R.anim.mi_laucher_del_down);
		up = AnimationUtils.loadAnimation(Login.this, R.anim.mi_laucher_del_up);
		down.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				imagevu.setVisibility(8);
			}
		});

		right = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, -1f,
				Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f);
		left = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1f, Animation.RELATIVE_TO_PARENT, 0f,
				Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f);
		right.setDuration(10000);
		left.setDuration(10000);
		right.setFillAfter(true);
		left.setFillAfter(true);

		right.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				imagevu.startAnimation(left);
			}
		});
		left.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				imagevu.startAnimation(right);
			}
		});
		imagevu.startAnimation(right);
	}
}
